﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

namespace ERPForms
{
    public partial class ContCardFrm : FrmCard
    {
        private String _contAdaugat = String.Empty;
        private String _contParinte = String.Empty;
        public String contAdaugat
        {
            get { return _contAdaugat; }
            set 
            {
                _contAdaugat = value;
                String[] bucati = _contAdaugat.Split('.');
                //cu exceptia ultimei portiuni
                for (int i = 0; i < bucati.Length - 1;i++ )
                {
                    if (_contParinte != String.Empty)
                        _contParinte += ".";
                    _contParinte += bucati[i].Trim();
                }
                if (_contParinte == String.Empty)
                    _contParinte = "N/A";
            }
        }

        private String _denCont = String.Empty;
        public String denCont
        {
            get { return _denCont;  }
            set {_denCont = value; }
        } 

        private Boolean adauga = false;
        public ContCardFrm(ModuleClass mc,Boolean _adaugare): base(mc)
        {
            adauga = _adaugare;
            InitializeComponent();
            workConnString = mc.workConnectionString;
            eSRPDataSet.Conturi.CodUnitateColumn.DefaultValue = ModuleInfo.CodUnitate;
            eSRPDataSet.Conturi.TerminalColumn.DefaultValue = true;
            eSRPDataSet.Conturi.EsteAnaliticColumn.DefaultValue = true;
            eSRPDataSet.Conturi.DebitInitialColumn.DefaultValue = Decimal.Zero;
            eSRPDataSet.Conturi.CreditInitialColumn.DefaultValue = Decimal.Zero;
            eSRPDataSet.Conturi.DebitPrecedentColumn.DefaultValue = Decimal.Zero;
            eSRPDataSet.Conturi.CreditPrecedentColumn.DefaultValue = Decimal.Zero;
            eSRPDataSet.Conturi.RulajCreditColumn.DefaultValue = Decimal.Zero;
            eSRPDataSet.Conturi.RulajDebitColumn.DefaultValue = Decimal.Zero;
            eSRPDataSet.Conturi.SoldColumn.DefaultValue = Decimal.Zero;

            Text = "Conturi Contabile";

        }

        private void ContCardFrm_Load(object sender, EventArgs e)
        {

            AddBindingSource(conturiBindingSource);
            listTableAdaptor.Add(conturiTableAdapter.Adapter);

            SqlConnection con = DeschideConexiune(workConnString);
            this.conturiTableAdapter.Connection = con;
            this.conturiTableAdapter.FillByCodUnitate(this.eSRPDataSet.Conturi,ModuleInfo.CodUnitate);

            this.tipContTableAdapter.Connection =con;
            this.tipContTableAdapter.Fill(this.eSRPDataSet.TipCont);
            if (adauga)
            {
                ///nu trebuie afisat nici un cont la adaugare
                conturiBindingSource.Filter = "CodUnitate=-17";
                Adauga(this, null);
                ESRPDataSet.ConturiRow cr = ((DataRowView)conturiBindingSource.Current).Row as ESRPDataSet.ConturiRow;
                contTB.Text = _contAdaugat;
                contParinteCB.Text=_contParinte ;
                if (contParinteCB.SelectedIndex != -1)
                {
                    contParinteCB.Enabled = _contParinte=="N/A";
                    ESRPDataSet.ConturiRow crp = ((DataRowView)contParinteCB.SelectedItem).Row as ESRPDataSet.ConturiRow;
                    cr.Tip = crp.Tip;
                    tipCB.SelectedValue = crp.Tip;
                    tipCB.Enabled = _contParinte == "N/A";
                    cr.Categorie = crp.Categorie;
                    cr.Denumire = _denCont;
                    denumireTB.Text = _denCont;
                }
            }
            InchideConexiune(con);
            
            HandleSalvare += Salvare;
            ActiveControl = denumireTB;

        }

        override public void Salvare(object sender, EventArgs args)
        {
            String cont = contTB.Text;
            base.Salvare(this, args);
            if (!EroareSalvare)
            {
                conturiBindingSource.Filter = "Cont = '" + cont + "'";
                currentRow = ((DataRowView)conturiBindingSource.Current);
                DialogResult = System.Windows.Forms.DialogResult.OK;
                Close();
            }
        }
        /*<metadata name="contLabel.GenerateMember" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
    <value>False</value>
  </metadata>
  <metadata name="denumireLabel.GenerateMember" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
    <value>False</value>
  </metadata>
  <metadata name="tipLabel.GenerateMember" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
    <value>False</value>
  </metadata>
  <metadata name="contParinteLabel1.GenerateMember" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
    <value>False</value>
  </metadata>
  <metadata name="eSRPDataSet.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
    <value>259, 17</value>
  </metadata>
  <metadata name="conturiBindingSource.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
    <value>380, 17</value>
  </metadata>
  <metadata name="conturiTableAdapter.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
    <value>549, 17</value>
  </metadata>
  <metadata name="tableAdapterManager.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
    <value>712, 17</value>
  </metadata>
  <metadata name="tipContBindingSource.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
    <value>1058, 17</value>
  </metadata>
  <metadata name="conturiBindingSource1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
    <value>883, 17</value>
  </metadata>
  <metadata name="tipContTableAdapter.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
    <value>1229, 17</value>
  </metadata>*/
    }
}
